package com.hua.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.hua.config.video.ConstantPropertiesUtils;
import com.hua.config.video.initVodClient;
import com.hua.service.VideoService;
import com.hua.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/video")
@CrossOrigin
public class VideoController {


    @Resource
    private VideoService videoService;

    //上传视频到阿里云
    @PostMapping("/uploadAlyuVideo")
    public Result uploadAlyuVideo(MultipartFile file){
        //返回上传视频id
        String videoId = videoService.uploadVideoAly(file);
        return Result.ok(videoId).message("上传成功");
    }


    @PostMapping("/getPlayAuth")
    public Result getPlayAuth(@RequestBody String id) {
        try {
            System.out.println(id);
            //创建初始化对象
            DefaultAcsClient client = initVodClient.initVodClient(ConstantPropertiesUtils.ACCESS_KEY_ID, ConstantPropertiesUtils.ACCESS_KEY_SECRET);
            //创建获取凭证request和response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            //向request设置视频id
            request.setVideoId(id);
            //调用方法得到凭证
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
            System.out.println(playAuth);
            return Result.ok(playAuth);
        }catch(Exception e) {
            return Result.Error().message("获取凭证失败");
        }
    }


}
